package yzserver.sx4a.positionType;

import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IPositionTypeDao;
import yzserver.sx4a.domain.PositionTypeDomain;
import yzserver.sx4a.form.PositionType;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aPositionTypeImpl implements Sx4aPositionType {

    private IPositionTypeDao positionTypeDao;

    public void setPositionTypeDao(IPositionTypeDao positionTypeDao) {
        this.positionTypeDao = positionTypeDao;
    }

    @Override
    public PositionType selPositionType(String positionTypeid) {
        PositionTypeDomain domain = positionTypeDao.selPositionType(positionTypeid);
        if(domain!=null){
            PositionType positionType = new PositionType(domain);
            return positionType;
        }else{
            return null;
        }

    }

    @Override
    public Ret addPositionType(PositionType positionType) {
        try {
            positionTypeDao.addPositionType(new PositionTypeDomain(positionType));
        } catch (Exception e) {
            return RetTools.getRet("创建岗位类型失败", e);
        }
        return RetTools.getRet("创建岗位类型成功", null);
    }

    @Override
    public Ret updPositionType(PositionType positionType) {
        try {
            positionTypeDao.updPositionType(new PositionTypeDomain(positionType));
        } catch (Exception e) {
            return RetTools.getRet("修改岗位类型失败", e);
        }
        return RetTools.getRet("修改岗位类型成功", null);
    }

    @Override
    public Ret delPositionType(String positionTypeid) {

        try {
            positionTypeDao.delPositionType(positionTypeid);
        } catch (Exception e) {
            return RetTools.getRet("删除岗位类型失败", e);
        }
        return RetTools.getRet("删除岗位类型成功", null);

    }

    @Override
    public List selAllPositionType(String positionTypeid) {
        List<PositionTypeDomain> positionTypeDomains = new ArrayList<PositionTypeDomain>();
        List  positionTypeList = new ArrayList();
        if("all".equalsIgnoreCase(positionTypeid)){ // all为接口约定的固定值
            positionTypeDomains = positionTypeDao.selAllPositionType(positionTypeid);
            if(positionTypeDomains!=null){
                for (int i = 0; i <positionTypeDomains.size(); i++) {
                    PositionType positionType = new PositionType(positionTypeDomains.get(i));
                    positionTypeList.add(positionType);
                }
            }
        }
        return positionTypeList;
    }
}
